﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace GenerateDB
{
    class DatabaseGenerator
    {
        class ColumnInfo
        {
            // Иия в БД
            public string Name;
            // Название колонки в программе.
            public string Text;
            // Тип данных в БД.
            public string Type;
            public bool SetInfo(string field, string value)
            {
                if (field == "name")
                    Name = value;
                else
                    if (field == "text")
                        Text = value;
                    else
                        if (field == "type")
                            Type = value;
                        else
                            return false;
                return true;
            }
        }
        class Journal
        {
            public string Name;
            public List<ColumnInfo> Columns;
        }
        public string PathToXML;
        private Journal journal;
        public void ParseXML()
        {
            try
            {
                journal = new Journal();

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(PathToXML);               
                XmlNodeList xmlNodeList = xmlDocument.GetElementsByTagName("loghog");
                if (xmlNodeList[0].FirstChild.Name != "name")
                    new XmlException();
                journal.Name = xmlNodeList[0].FirstChild.InnerText;
                journal.Columns = new List<ColumnInfo>();
                xmlNodeList = xmlDocument.GetElementsByTagName("column");
#if DEBUG
                Console.WriteLine("Имя журнала: " + journal.Name);
                Console.WriteLine("Колонки: ");
#endif
                foreach (XmlNode xmlNode in xmlNodeList)
                {
                    ColumnInfo columnInfo = new ColumnInfo();
                    foreach (XmlNode xmlChildNode in xmlNode.ChildNodes)
                    {
                        columnInfo.SetInfo(xmlChildNode.Name, xmlChildNode.InnerText);
#if DEBUG
                        Console.WriteLine("Имя: {0}, Тип: {1}", xmlChildNode.Name, xmlChildNode.InnerText);
#endif
                    }
                    journal.Columns.Add(columnInfo);
                }
            }
            catch (XmlException ex)
            {
                Console.WriteLine("-err. Exception: {0}, TargetSite: {1}, StackTrace: {2}", ex.Message, ex.TargetSite, ex.StackTrace);
            }
        }
        public void Generate()
        {
            CreateJournal(journal.Name);
            foreach (ColumnInfo columnInfo in journal.Columns)
            {
                AddColumn(columnInfo.Name, columnInfo.Type);
            }
        }

        private void AddColumn(string name, string type)
        {
            throw new NotImplementedException();
        }

        private void CreateJournal(string name)
        {
            throw new NotImplementedException();
        }
    }
}
